﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.IO;
using System.Globalization;

namespace WebMacros
{
    public class script2
    {
        class Subject
        {
            public List<uint> ID { get; set; }
            public Point Current { get; set; }
            public Subject(Point pt)
            {
                Current = pt;
                ID = new List<uint>();
            }
        }

        private string m_id;
        private string m_password;

        private Dictionary<string, Subject> m_subjects = new Dictionary<string, Subject>();

        public script2( TextBox logbox, string id, string password )
        {
            m_id = id;
            m_password = password;
            
            m_subjects["교육사회학"] = new Subject(new Point(56, 385));
            m_subjects["상담심리학"] = new Subject(new Point(56, 415));
            m_subjects["아동가족의이해"] = new Subject(new Point(56, 440));
            m_subjects["아동발달"] = new Subject(new Point(56, 466));

            Main(logbox);

            logbox.Invoke( new MethodInvoker( delegate()
            {
                logbox.AppendText( "완료!!" );
            } ) );
        }

        public void Main(TextBox logbox)
        {
            Scripter tab1 = new Scripter( 1, "", true );            

            tab1.GotoURL( "http://lcms.kwu.or.kr/" );
            tab1.Size( 1067, 868 );
            tab1.Wait( 1.5f );
            tab1.Line( string.Format( "TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:logFrm ATTR=NAME:txt_login_id CONTENT={0}", m_id ) );
            tab1.Line( "SET !ENCRYPTION NO" );
            tab1.Line( string.Format( "TAG POS=1 TYPE=INPUT:PASSWORD FORM=NAME:logFrm ATTR=NAME:txt_login_password CONTENT={0}", m_password ) );

            // 로그인 버튼
            tab1.Click( 158, 180 );

            tab1.Wait( 1.5f );
            tab1.Play();

            foreach( KeyValuePair<string, Subject> subject in m_subjects )
            {
                logbox.Invoke(new MethodInvoker(delegate()
                {
                    logbox.AppendText(string.Format("이번 과목 '{0}' \r\n", subject.Key));
                }));

                tab1.Line("CODE:TAB T=1");
                tab1.Line("TAB CLOSEALLOTHERS");
                tab1.GotoURL("http://lcms.kwu.or.kr/");
                tab1.Size(1067, 868);

                // 나의 강의실
                tab1.Click(subject.Value.Current.X, subject.Value.Current.Y);
                tab1.Wait(1.5f);

                // 강의실 입장
                tab1.Click(93, 260);
                tab1.Wait(1.5f);

                // 강의 데이터 가져오기
                tab1.Line("TAG POS=17 TYPE=TABLE ATTR=TXT:* EXTRACT=TXT");

                tab1.Play();

                // 과목 처리
                List<uint> ids = MainParser(tab1.Extract(1));

                foreach (uint id in ids )
                {
                    tab1.Line("CODE:TAB T=1");
                    tab1.Line("TAB CLOSEALLOTHERS");
                    tab1.Wait(3.0f);
                    tab1.Line(string.Format("TAG POS={0} TYPE=IMG FORM=NAME:lecture_form ATTR=SRC:http://lcms.kwu.or.kr/images/lecture/button_attend.gif", id));
                    tab1.Wait(3.0f);
                    tab1.Play();

                    //Scripter tab2 = new Scripter( 2, "", false );
                    tab1.Line("CODE:TAB T=3");
                    tab1.Wait(5.0f);
                    tab1.Line("SET !ERRORIGNORE YES");

                    for (int i = 0; i < 6; ++i)
                    {
                        // Next Button
                        tab1.Click(588, 741);
                        tab1.Line("ONDIALOG POS=1 BUTTON=YES CONTENT=");
                        tab1.Wait(15.0f);
                        tab1.Wait(3.0f);
                    }

                    // 본강의 (학습하기 버튼)
                    tab1.Click(756, 69);
                    tab1.Wait(1900.0f);
                    tab1.Wait(3.0f);
                    tab1.Line("TAB CLOSE");
                    tab1.Play(500000);
                }
            }

            tab1.Exit();
        }

        public List<uint> MainParser( string extract )
        {
            List<uint> ids = new List<uint>();

            uint idCount = 0;
            string[] lines = extract.Split( new string[] { "#NEWLINE##NEWLINE#" }, StringSplitOptions.None );
            foreach( string line in lines )
            {
                string[] data = line.Split( new string[] { "#NEXT#" }, StringSplitOptions.None );

                if( data.Count() == 6 )
                {
                    CultureInfo cu = new CultureInfo( "ko-KR" );

                    string[] temp = data[1].Split( '~' );
                    DateTime startTime = DateTime.ParseExact( temp[0].Trim(), "yyyy-MM-dd", cu );
                    DateTime endTime = DateTime.ParseExact( temp[1].Trim(), "yyyy-MM-dd", cu );

                    if( startTime <= DateTime.Today
                        && endTime >= DateTime.Today )
                    {
                        ++idCount;

                        string bUse = data[4].Trim().ToUpper();
                        if( bUse == "X" )
                        {
                            ids.Add( idCount );
                        }
                    }
                }
            }

            return ids;
        }
    }
}
